package com.czl.module_service.adapter.asset

import android.os.Bundle
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.czl.base.binding.command.BindingCommand
import com.czl.base.binding.command.BindingConsumer
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.util.ToastHelper.showNormalToast
import com.czl.module_service.R
import com.czl.module_service.databinding.ItemAssetStoreApplyAddSortBinding
import com.czl.module_service.fragment.assetUse.AssetStoreAllocationApplyAddFragment
import java.util.ArrayList

class AssetStoreAllocationApplyAddAdapter(private val fragment: AssetStoreAllocationApplyAddFragment) :
    BaseQuickAdapter<SortBean, BaseDataBindingHolder<ItemAssetStoreApplyAddSortBinding>>(
        R.layout.item_asset_store_apply_add_sort
    ) {

    var storehouseId : Int? = null

    var position : Int = 0

    var isDel : Boolean = false

    override fun convert(
        holder: BaseDataBindingHolder<ItemAssetStoreApplyAddSortBinding>,
        item: SortBean,
    ) {
        item.isDel = isDel
        item.position = holder.layoutPosition
        holder.dataBinding?.run {
            data = item
            adapter = this@AssetStoreAllocationApplyAddAdapter
            executePendingBindings()
        }
    }

    val chooseSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (storehouseId != null) {
            if (it is SortBean) {
                position = it.position!!
                data.filter { e -> e.sortId != null }
                val sortIds : MutableList<Int> = arrayListOf()
                data.forEach { e ->
                    if (e.sortId != null) {
                        sortIds.add(e.sortId!!)
                    }
                }
                fragment.startContainerActivity(AppConstants.Router.Service.F_SERVICE_SORT_CHOOSE_LIST, Bundle().apply {
                    putSerializable(AppConstants.BundleKey.STORE_HOUSE_KEY, storehouseId)
                    putSerializable("sortIds", sortIds as ArrayList<Int>)
                })
            }
        } else {
            showNormalToast("请选择调出仓库")
        }

    })

    val delSortClick: BindingCommand<Any> = BindingCommand(BindingConsumer {
        if (it is SortBean) {
            fragment.deleteList(it)
//            remove(it)
        }
    })
}
